package futianyu;
import java.io.*;
public class Serialization {

        public static void main(String[] args) {
            Student student = new Student("小王", 18, "男", 90, 1001, 2022);

            // 序列化到文件
            try (ObjectOutputStream oos = new ObjectOutputStream(
                    new FileOutputStream("student.ser"))) {
                oos.writeObject(student);
                System.out.println("对象已成功序列化到student.ser文件");
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 反序列化示例
            try (ObjectInputStream ois = new ObjectInputStream(
                    new FileInputStream("student.ser"))) {
                Student deserializedStudent = (Student) ois.readObject();
                System.out.println("反序列化后的对象信息：");
                System.out.println("姓名：" + deserializedStudent.getName());
                System.out.println("年龄：" + deserializedStudent.getAge());
                System.out.println("性别：" + deserializedStudent.getSex());
                System.out.println("分数：" + deserializedStudent.getScore());
                System.out.println("编号：" + deserializedStudent.getNum());
                System.out.println("年级：" + deserializedStudent.getGrade());

            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }


